<?php
namespace backend\modules\enterprise\controllers;

use backend\controllers\BaseController;
use backend\models\EnterpriseSearch;
use common\models\Enterprise;
use common\traits\DataProviderPagination;
use Yii;
use yii\web\NotFoundHttpException;

class IndexController extends BaseController
{
    use DataProviderPagination;

    /**
     * @return \common\models\Enterprise
     * @throws \Exception
     */
    public function actionCreate()
    {
        $model = new Enterprise();
        $model->load(Yii::$app->getRequest()->getBodyParams(), '');

        return $model->save();
    }

    /**
     * @return array
     */
    public function actionIndex()
    {

        $requestParams = Yii::$app->getRequest()->getQueryParams();
        return $this->returnPagination((new EnterpriseSearch())->search($requestParams));
    }

    /**
     * @param $id
     *
     * @return false|int
     * @throws \Exception | \Throwable
     */
    public function actionDelete($id)
    {
        return $this->findModel($id)->delete();
    }

    /**
     * @param $id
     *
     * @return \common\models\Enterprise
     * @throws \Exception
     */
    public function actionUpdate($id)
    {
        $model= $this->findModel($id);
        $model->load(Yii::$app->getRequest()->getBodyParams(), '');

        return $model->save();
    }

    /**
     * @param $id
     *
     * @return \common\models\Enterprise
     * @throws \Exception
     */
    protected function findModel($id)
    {
        if (($model = Enterprise::findOne($id)) !== null) {
            return $model;
        }
        throw new NotFoundHttpException('The requested api does not exist.');
    }

}